package org.zcl.elastic.dao;

import com.fasterxml.jackson.databind.node.ObjectNode;
import org.springframework.stereotype.Service;
import org.zcl.elastic.entity.Scroll;
import org.zcl.elastic.entity.ScrollDeepRequest;
import org.zcl.elastic.entity.ScrollRequest;
import org.zcl.elastic.entity.User;

import java.util.ArrayList;
import java.util.List;

/**
 * .
 *
 * @author zhuchenglin
 * @since 2024/9/8 21:19
 */
@Service
public class AppEsRepository extends EsRepository {
    private final static int SCROLL_SIZE = 20;

    public Scroll<ObjectNode> findScroll(ScrollRequest plan) {
        int scrollSize = plan.getScrollSize() == 0 ? SCROLL_SIZE : plan.getScrollSize();
        return findScroll(plan.getIndex(), plan.getFields(), scrollSize, plan.getPageable().getSort(), plan.getQueryBuilder(), ObjectNode.class);
    }

    public Scroll<ObjectNode> findScroll(String scrollId) {
        return findScroll(scrollId, ObjectNode.class);
    }


    public List<User> findDeepSearch(ScrollDeepRequest request, Class objectClass) {
        List<User> results = new ArrayList<>();
        fetchAllResults(request.getIndex(), request.getSortField(), request.getSortOrder(), request.getSearchAfterValue(), objectClass, results);
        return results;
    }
}
